package totalFruit.java;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class totalFruit {
    public static void main(String[] args) {
        int[] nums = new int[]{3,3,3,1,2,1,1,2,3,3,4};
        System.out.println(totalFruit(nums));
    }

    public static int totalFruit(int[] fruits) {
        int left = 0;
        Map<Integer,Integer> map = new HashMap<>();
        int total = 0;
        for (int right = 0; right < fruits.length; right++) {
            map.put(fruits[right], map.getOrDefault(fruits[right], 0) + 1);
            while (map.size() > 2) {
                map.put(fruits[left], map.get(fruits[left]) - 1);
                if (map.get(fruits[left]) == 0) {
                    map.remove(fruits[left]);
                }
                left++;
            }
            total = Math.max(total, right - left + 1);
        }
        return total;
    }
}
